﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using System.Web.Services;
using RedHouse.Data.Common;
using RedHouse.Data.Entities;

namespace RedHouse.AdminControl.UserControls
{
    public partial class UCCategory : System.Web.UI.UserControl
    {
        private ICategoryService _categoryService;
        public static List<RedHouseCategory> Categories { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            cboGroup.SelectedIndexChanged += new EventHandler(cboGroup_SelectedIndexChanged);
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        void cboGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            rptCategory.DataSource = Categories.Where(c => c.Group == int.Parse(cboGroup.SelectedValue)).OrderBy(c=>c.Title).ToList();
            rptCategory.DataBind();
        }

        private void Initialize()
        {
            _categoryService = new CategoryService();

            Categories = _categoryService.GetAll().OrderBy(c => c.Title).ToList();
            
            cboGroup.DataSource = RedHouseGroup.GroupList;
            cboGroup.DataTextField = "Name";
            cboGroup.DataValueField = "Value";
            cboGroup.DataBind();
            cboGroup.SelectedIndex = 0;

            rptCategory.DataSource = Categories.Where(c => c.Group == int.Parse(cboGroup.SelectedValue)).ToList();
            rptCategory.DataBind();
        }
    }
}